package demo;



import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List;

import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import lib.ConnectionWeb;
import lib.OneMenuHasClickFilter;
import lib.OneMenuNoClickFilter;
import lib.StringEdit;
import lib.TwoMenuFilter;

public class demo3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String url="http://www.vinabook.com/";
		ConnectionWeb con= new ConnectionWeb();
		String xpath="tr/td/h1";
		String xp="div align=\"justify\" style=\"padding-left:30px; line-height:15px\"/p";
		String xpp="td/div/p";
		xpp=StringEdit.EditXpath(xpp);
		TagNode root=con.getInfo(url);
		//List<String> l= con.getString(xpath);
		List<String> name;
		List<String> link;
		Object[] l3;
		try {
			l3=root.evaluateXPath(xpp);
		} catch (XPatherException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		List<?> l2 = root.getElementListByAttValue("id","menu", true,true);
		
		List<TagNode> l=con.executeXpath(root, xpath);
		
		for(int i=0; i< l.size();i++)
		{
			TagNode t=l.get(i);
			Writer out2;
			
				try {
					out2 = new BufferedWriter(new OutputStreamWriter(
							new FileOutputStream("E:\\noidung.txt",true),"UTF-8"));
					out2.write(t.toString());
					out2.close();
				} catch (UnsupportedEncodingException | FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				
			
			
		}
		List<TagNode> l22=con.executeXpath2(root, xp);
	//	TwoMenuFilter fil= new TwoMenuFilter();
	//	OneMenuHasClickFilter fil=new OneMenuHasClickFilter();
		OneMenuNoClickFilter fil=new OneMenuNoClickFilter();
		fil.lay_noi_dung_web2(url, xpath, xp);
		name=fil.getTopicDic();
		link=fil.getLink();
		for(int j= 0;j<name.size();j++)
		{	
			System.out.print("a");
			System.out.print(j);
			Writer out1;
			try {
				out1 = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream("E:\\sahara.txt",true),"UTF-8"));
				out1.write(name.get(j));
				out1.write(System.getProperty( "line.separator" ));
				out1.write(link.get(j));
				out1.write(System.getProperty( "line.separator" ));
				out1.write(System.getProperty( "line.separator" ));
				out1.close();
			} catch (UnsupportedEncodingException | FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
			
		}
	
	}

}
